using System;
using System.Data;
using System.IO;
using System.Windows.Forms;
using IPAMS.Data;

namespace IPAMS.Views
{
    /// <summary>
    /// This is an editor for the Currencies table. It enables loading from a file,
    ///saving to a file and more
    /// </summary>
    public partial class CurrenciesEditor : UserControl
    {
        public IPAMSDataSet.CurrencyDataTable currenciesTable;
        string[] currenciesHeaders;

        /// <summary>
        /// Initializes a new instance of the <see cref="CurrenciesEditor"/> class.
        /// </summary>
        public CurrenciesEditor(IPAMSDataSet dataSet)
		{
			currenciesTable = dataSet.Currency;
            InitializeComponent();
            InitializeComponentExtneded();
        }

        /// <summary>
        /// Initializes the component extneded.
        /// </summary>
        private void InitializeComponentExtneded()
        {
           dgvCurrencies.DataSource = currenciesTable;
		   foreach (DataGridViewColumn column in dgvCurrencies.Columns)
		   {
			   column.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
		   }
        }

        /// <summary>
        /// Handles the DataSourceChanged event of the dgvCurrencies control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void dgvCurrencies_DataSourceChanged(object sender, EventArgs e)
        {
            dgvCurrencies.Refresh();
        }

/*      /// <summary>
        /// Loads the data from a file.
        /// </summary>
        /// <param name="dataFilePath">The data file path.</param>
        /// <returns>a DataTable with the file's data</returns>
        public DataTable LoadData(string dataFilePath)
        {

            if (File.Exists(dataFilePath))
            {
                StreamReader sr = new StreamReader(dataFilePath);
                currenciesHeaders = sr.ReadLine().Split(',');;
                while (true)
                {
                    string currency = sr.ReadLine();
                    if (currency == string.Empty || currency == null) break;
                    string[] currencyInfo = currency.Split(',');
                    if (currencyInfo.Length != 3) continue;
                    currenciesTable.AddDTCurrenciesRow(currencyInfo[0], currencyInfo[1], currencyInfo[2]);
                }
                sr.Close();
                Text = dataFilePath;

             currenciesTable.AcceptChanges();
             dgvCurrencies.DataSource = currenciesTable;
             for (int index = 0; index < currenciesHeaders.Length; index++)
             {
                 dgvCurrencies.Columns[index].HeaderText = currenciesHeaders[index];
             }



             foreach (DataGridViewColumn column in dgvCurrencies.Columns)
             {
                 column.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
             }
            }
         

            return currenciesTable;
        }*/

        /// <summary>
        /// Saves the data to a file.
        /// </summary>
        /// <param name="dataFilePath">The data file path.</param>
        public void SaveData(string dataFilePath)
        {
            StreamWriter sw = new StreamWriter(dataFilePath);
            string fileContent = currenciesHeaders + Environment.NewLine;
            foreach (DSIPAMS.DTCurrenciesRow currency in currenciesTable.Rows)
            {
                fileContent += string.Format("{0},{1},{2}" + Environment.NewLine, currency[0], currency[1], currency[2]);
            }
            sw.Write(fileContent);
            sw.Close();

        }

    }
    
}
